package net.midnighttd.game.view;

import net.midnighttd.engine.entity.Entity;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/**
 * Author: Adam Scarr
 * Date: 21/04/2009
 * Time: 1:33:16 PM
 */
public class EntityButton extends ThemedButton implements MouseListener {
    private Icon inactive;
    private Icon hover;
    private Icon active;
    private Icon disabled;
    private int status = 0;
    public static final int INACTIVE = 0;
    public static final int ACTIVE = 1;
    public static final int DISABLED = 2;
    private Entity ent;

    public EntityButton(Entity e) {
        ent = e;
        Image towerImg = e.getImage();

        inactive = iconifyAndCorrect(towerImg, 0, 0, 0, 0);
        hover = iconifyAndCorrect(towerImg, 1, 1, 1, 0.1f);
        active = iconifyAndCorrect(towerImg, 0.6f, 1, 0.5f, 0.2f);
        disabled = iconifyAndCorrect(towerImg, 0.7f, 0.7f, 0.7f, 0.7f);

        setIcon();
        
        addMouseListener(this);

        setMargin(new Insets(1,1,1,1));
    }

    private void setIcon() {
        switch(status) {
            case DISABLED:  setIcon(disabled);  break;
            case ACTIVE:    setIcon(active);    break;
            case INACTIVE:  setIcon(inactive);  break;

        }
    }

    public void setStatus(int status) {
        this.status = status;
        setIcon();
    }

    private Icon iconifyAndCorrect(Image i, float r, float g, float b, float a) {
        BufferedImage img = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics gfx = img.getGraphics();

        gfx.drawImage(i, 0, 0, null);

        gfx.setColor(new Color(r, g, b, a));

        gfx.fillRect(0, 0, i.getWidth(null), i.getHeight(null));

        return new ImageIcon(img);
    }

    public void mouseClicked(MouseEvent e)  {}
    public void mousePressed(MouseEvent e)  {}
    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {
        setIcon(hover);
    }

    public void mouseExited(MouseEvent e) {
        setIcon();
    }

    public Entity getEntity() {
        return ent;
    }
}
